<script setup lang="ts">

import {ref, onMounted} from 'vue'
import type { UploadInstance } from 'element-plus'
import { ElMessage } from 'element-plus'
import router from '@/router';


import {apiMajorList, apiSkillList,apiStandardsSave} from "@/api/api"
import {backTo} from '@/utils/jump'

const uploadRef = ref<UploadInstance>()

const standardCreate= ref({
	standardName:"",
	mainMajorId:0,
	mainMajorName:"",
	majorId:0,
	majorName:"",
	subMajorId:0,
	subMajorName:"",
	skillExtractOn:1
})




const majorOpt =ref([])
const majorMap = new Map()
async function loadMajorList(){
	const data =  await apiMajorList()
	if (data.code != 0) {
	      console.log(data)
	      ElMessage.error("执行失败， 请联系管理员")
	      return 
	 }
	 data.rows.forEach((mMajor)=>{
	 	const mOpt = {
	 		value:mMajor.mainMajorId,
	 		label:mMajor.mainMajorName,
			children:[],
	 	}


	 	mMajor.childrenData.forEach((sMajor)=>{
	 		const sOpt = {
	 			value:sMajor.subMajorId,
				label: sMajor.subMajorName,
				children:[],
	 		}
	 		sMajor.childrenData.forEach((major)=>{
	 			const opt = {
		 			value:major.majorId ,
					label:major.majorName ,		
	 			}
	 			majorMap.set(major.majorId,major.majorName)
	 			sOpt.children.push(opt)
	 		})
	 		mOpt.children.push(sOpt)
	 		majorMap.set(sMajor.subMajorId,sMajor.subMajorName)

	 	})
	 	majorOpt.value.push(mOpt)
		majorMap.set(mMajor.mainMajorId,mMajor.mainMajorName)

	 })

}

const handlerSelectFile = (file,files)=>{
	if (standardCreate.value.standardName == ""){
		standardCreate.value.standardName = file.name
	}
}

const handleMajorChange = (value) => {
	standardCreate.value.mainMajorId= value[0]
	standardCreate.value.mainMajorName= majorMap.get(value[0])

	standardCreate.value.subMajorId= value[1]
	standardCreate.value.subMajorName= majorMap.get(value[1])

	standardCreate.value.majorId= value[2]
	standardCreate.value.majorName= majorMap.get(value[2])
	

}

const paramsSkillList = ref({
	pageSize:100,
	pageNum:1, 
})
const skills = ref([])
async function loadSkillList(){
	const data =  await apiSkillList(paramsSkillList.value)
	if (data.code != 0) {
	      console.log(data)
	      ElMessage.error("执行失败， 请联系管理员")
	      return 
	 }
	 skills.value = data.rows
	 paramsSkillList.value.total = data.total
}
const checkAllSkills = ref(false)
const isIndeterminate = ref(true)
const checkedSkills = ref([])
const handleSkillCheckAllChange =(val) =>{
	checkedSkills.value = val ? skills.value : []
  	isIndeterminate.value = false
}
const handleCheckedSkillChange = (value) => {
  const checkedCount = value.length
  checkAllSkills.value = checkedCount === skills.length
  isIndeterminate.value = checkedCount > 0 && checkedCount < skills.length
}

async function commit(){
	const params = new Map()

	checkedSkills.value.forEach((v, index)=>{
		params.set("skillList["+index+"].skillId", v.id)
		params.set("skillList["+index+"].skillName", v.skillName)
	})

	params.set( "mainMajorId", standardCreate.value.mainMajorId)
	params.set( "mainMajorName", standardCreate.value.mainMajorName)
	params.set( "majorId", standardCreate.value.majorId)
	params.set( "majorName", standardCreate.value.majorName)
	params.set( "skillExtractOn", standardCreate.value.skillExtractOn)
	params.set( "standardName", standardCreate.value.standardName)
	params.set( "subMajorId", standardCreate.value.subMajorId)
	params.set( "subMajorName", standardCreate.value.subMajorName)



  	const file = document.querySelector('input[type=file]').files[0]

  	if (file == null) {
  		ElMessage.error('标准文件未选择， 请选择需要分析的文件')
  		return
  	}
  	if (standardCreate.value.standardName == "") {
  		ElMessage.error('请输入标准名称')
  		return
  	}
	if (standardCreate.value.majorId == 0) {
  		ElMessage.error('请选择专业')
  		return
  	}

	const  resp =  await  apiStandardsSave(file, params)
	if (resp.code != 0) {
	      console.log(resp)
	      ElMessage.error("执行失败， 请联系管理员")
	      return 
	 }
	ElMessage({
    	message: '导入成功！',
    	type: 'success',
  	})
    router.push({ path: '/standard/list' })


}

onMounted(() => {
  loadMajorList()
  loadSkillList()
})


</script>

<template>

<el-button  type="primary" @click="backTo" >返回</el-button>

<div class="info_box">
	<el-row :gutter="20">
    <el-col :span="10">
    	<el-descriptions title="课程基础信息" column="1" >
	    <el-descriptions-item label="名称:">
	    	<template #default>
	    		<div class="box_inline" >
	    			<el-input   v-model="standardCreate.standardName" placeholder="课程名称" clearable />			
	    		</div>
	    	</template>
	    </el-descriptions-item>
	    <el-descriptions-item label="专业:">
	    	  <el-cascader        @change="handleMajorChange" filterable :options="majorOpt" clearable placeholder="专业"/>
	    </el-descriptions-item>
	    <el-descriptions-item label="绑定知识点/技能点:">

		  <el-checkbox
		  	v-model="checkAllSkills"
		    :indeterminate="isIndeterminate"
		    @change="handleSkillCheckAllChange"
		    >选择全部</el-checkbox
		  >
	    </el-descriptions-item>
	  </el-descriptions>
    </el-col>
    <el-col :span="10">
    	<div class="box_upload" >
    	<el-upload
			ref="uploadRef"
			:multiple:="false"
			:auto-upload="false"
			:on-change="handlerSelectFile"
			>
			<template #trigger>
				<el-button type="primary">选择课程标准文件</el-button>
			</template>
		</el-upload>
    	</div>
    </el-col>
  </el-row>
  	
  <div class="box_local">
		  <el-checkbox-group
		    v-model="checkedSkills"
		    @change="handleCheckedSkillChange"
		  >
		    <el-checkbox v-for="skill in skills"  :label="skill" >{{
		      skill.skillName
		    }}</el-checkbox>
		  </el-checkbox-group>

 <el-pagination class='pagination' :page-size="paramsSkillList.pageSize" layout=" prev, pager, next, total" :total="paramsSkillList.total" v-model:current-page="paramsSkillList.pageNum" @current-change="loadSkillList" />

  </div>

</div>
<el-button class="sub_button"  type="primary" @click="commit" >提交</el-button>


</template>

<style scoped>

.box_inline{
	display: inline-block;
	width: 40%;
}

.box_upload{
	padding-top: 2.5em;
}
.box_cascader{
	width: 100%;
}

.sub_button{
	float: right;

}
.pagination {
  float: right;
}

.box_local{
	padding: 1em;
}

</style>